package cn.Alibaba.basic.controller;

import cn.Alibaba.basic.dto.SmsCodeDto;
import cn.Alibaba.basic.exception.BusinessException;
import cn.Alibaba.basic.service.IVerifyService;
import cn.Alibaba.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyService verifyService;


    @GetMapping("/image/{key}")
    public JsonResult getImgCode(@PathVariable("key") String key){

        try {
            String base64Str = verifyService.getImgCode(key);
           return JsonResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            return JsonResult.me().setMsg("系统异常 请稍后重试！");
        }


    }


    @PostMapping("/smsCode")
    public JsonResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto){

        try {
            verifyService.sendSmsCode(smsCodeDto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常 ，请稍后重试！");
        }

    }

    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto){

        try {
            verifyService.binderSmsCode(smsCodeDto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常 ，请稍后重试！");
        }

    }





}
